package com.zj.core.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.support.constraint.ConstraintLayout
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView

import com.bumptech.glide.Glide
import com.zj.core.R
import com.zj.core.bean.NurseRecResBean
import kotlinx.android.synthetic.main.item_record_detail_nurse_list.view.*

/**
 * 日期：2019年3月25日
 * 公司：北京子杰宝贝科技有限公司
 * 地址：北京市朝阳区朝阳农场
 * 开发者：达选文
 *
 *
 * 详细封装内容：历史记录适配器
 */
class NurseListAdapter(context: Context?, detailList: MutableList<NurseRecResBean.DatEntity.RowsEntity>?):RecyclerView.Adapter<NurseListAdapter.RecordOcrAdapterHolder>() {
    private var mDetailList:MutableList<NurseRecResBean.DatEntity.RowsEntity>?=detailList
    private var mListener: OnItemListener? = null
    private var mContext:Context?=context

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecordOcrAdapterHolder {
        return RecordOcrAdapterHolder(LayoutInflater.from(mContext).inflate(R.layout.item_record_detail_nurse_list, parent, false))
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: RecordOcrAdapterHolder, position: Int){
        with(holder){
            var mesStatus=""
            this.recordOcrView.setOnClickListener { mListener!!.onClick(mDetailList!![position], position) }
            val bean = mDetailList!![position]
            Glide.with(mContext).load(bean.image).error(R.mipmap.error_load_img).into(this.ocrImgView)
            if(bean.status!=null&&bean.status!=""){
                mesStatus=bean.status.toString()
            }
            this.recordContentTitleTv.text = bean.voice + bean.measure?.replace("\n","").toString()+mesStatus
            this.recordTimeTv.text = bean.systime
            this.recordUserTv.text = bean.nurseid
        }
    }

    /**
     * 加载更多
     *
     * @param record
     */
    fun onLoadMore(record: List<NurseRecResBean.DatEntity.RowsEntity>) {
        for (mRecordBean in record) {
            mDetailList?.add(mRecordBean)
        }
        notifyDataSetChanged()
    }

    /**
     * 刷新
     */
    fun onRefush(beanList: MutableList<NurseRecResBean.DatEntity.RowsEntity>) {
        this.mDetailList = beanList
        notifyDataSetChanged()
    }

    /**
     * 设置Item点击监听
     *
     * @param listener
     */
    fun setItemClickListener(listener: OnItemListener) {
        this.mListener = listener
    }

    interface OnItemListener {
        fun onClick(mRecordBean: NurseRecResBean.DatEntity.RowsEntity, index: Int)
    }

    override fun getItemCount(): Int {
        return mDetailList!!.size
    }

    inner class RecordOcrAdapterHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
        var recordOcrView: ConstraintLayout=itemView.record_ocr_view
        var recordTimeTv: TextView= itemView.record_time_tv
        var recordUserTv: TextView=itemView.record_type_tv
        var ocrImgView: ImageView=itemView.ocr_img_view
        var recordContentTitleTv: TextView=itemView.record_content_title_tv
    }

    fun clean(){
        if(mDetailList!=null){
            mDetailList?.clear()
            mDetailList=null
        }

        if(mListener!=null){
            mListener=null
        }

        if(mContext!=null){
            mContext=null
        }
    }
}
